package com.xianyun.book.auth.controller;

import com.alibaba.cola.dto.Response;
import com.xianyun.book.api.SystemUserServiceI;
import com.xianyun.book.common.constant.BizCode;
import com.xianyun.book.common.util.StringUtils;
import com.xianyun.book.dto.UserRegisterCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author macos-zyj
 */
@Slf4j
@RestController
public class RegisterController {
    @Autowired
    private SystemUserServiceI userService;
    /**
     * 用于密码加解密
     */
    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("/open/api/register/user")
    public Response registerUser(UserRegisterCmd saveCmd){
        saveCmd.setBizId(BizCode.BIZ_ID_BOOK);
        saveCmd.setScenario(BizCode.USER_SCENARIO);
        return userService.registerUser(saveCmd);
    }

    @PostMapping("/open/api/register/manager")
    public Response registerManager(UserRegisterCmd saveCmd){
        saveCmd.setBizId(BizCode.BIZ_ID_BOOK);
        saveCmd.setScenario(BizCode.MANAGER_SCENARIO);
        if (StringUtils.isNotEmpty(saveCmd.getPassword())){
            saveCmd.setPassword(passwordEncoder.encode(saveCmd.getPassword()));
        }
        return userService.registerUser(saveCmd);
    }
}
